﻿Public Class xtf_ABMconfig_empresa

    Dim ulg_row As DataRow
    Dim obj_config_empresa As cls_config_empresa = Nothing

    Public Sub New()
        InitializeComponent()
        CargarPais_()
        CargarProvincia_()
        Cargarcondicioniva_()
        cargarCombo(cbo_fiscal_impresiontipo, GetType(cls_config_empresa.enu_impresiontipo))
        cargarCombo(cbo_fiscal_impresorafiscal_modelo, GetType(cls_config_empresa.enu_fiscalmodelo))
        For Each puerto As String In obtenerPuertosSerieCOM()
            cbo_fiscal_impresorafiscal_puerto.Properties.Items.Add(puerto)
        Next
        AgregarValidaciones()
    End Sub


    Function obtenerPuertosSerieCOM() As List(Of String)
        Dim puertosSerie As List(Of String)

        puertosSerie = New List(Of String)
        Try
            puertosSerie = New List(Of String)
            For Each puertosSerieObtenidos As String In My.Computer.Ports.SerialPortNames
                puertosSerie.Add(puertosSerieObtenidos)
            Next
            obtenerPuertosSerieCOM = puertosSerie
        Catch ex As Exception
            Throw New Exception("Error al obtener los puertos serie." & Chr(13) & _
          "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
          "Tipo de exepcion: " & ex.GetType.ToString & "." & Chr(13) & _
          "Descripción: " & ex.Message)
        End Try
    End Function

    Public Overrides Sub CargarObjetos()
        'CargarPais_()
    End Sub

    Private Sub CargarPais_()
        Dim pais_ As New cls_pais_
        pais_.CargarTodos()
        cargarCombo(cbo_Pais, pais_.Items, False)
        cbo_Pais.EditValue = Nothing
    End Sub

    Private Sub CargarProvincia_()
        Dim provincia_ As New cls_provincia_
        provincia_.CargarXidpais(cbo_Pais.EditValue)
        cargarCombo(cbo_Provincia, provincia_.Items, False)
        cbo_Provincia.EditValue = Nothing
    End Sub


    Private Sub Cargarcondicioniva_()
        Dim condicioniva_ As New cls_condicioniva_
        condicioniva_.CargarTodos()
        cargarCombo(cbo_fiscal_idcondicioniva, condicioniva_.Items, False, True)
    End Sub

    Private Sub cbo_Pais_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_Pais.EditValueChanged
        CargarProvincia_()
    End Sub

    Public Shadows Sub Modificar(ByRef config_empresa As cls_config_empresa, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_config_empresa) Then
            obj_config_empresa = New cls_config_empresa
        End If
        obj_config_empresa = config_empresa
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = "Datos de Empresa"
  
        txt_razonsocial.Text = obj_config_empresa.razonsocial
        txt_domicilio.Text = obj_config_empresa.domicilio
        txt_nombrefantasia.Text = obj_config_empresa.nombrefantasia
        txt_web.Text = obj_config_empresa.web
        txt_horario.Text = obj_config_empresa.horario
        txt_email.Text = obj_config_empresa.email
        txt_celular.Text = obj_config_empresa.celular
        txt_fax.Text = obj_config_empresa.fax
        txt_telefono.Text = obj_config_empresa.telefono
        cbo_Pais.EditValue = obj_config_empresa.idpais
        cbo_Provincia.EditValue = obj_config_empresa.idprovincia
        txt_logochico.Text = obj_config_empresa.logochico
        txt_logogrande.Text = obj_config_empresa.logogrande


        txt_fiscal_razonsocial.Text = obj_config_empresa.fiscal_razonsocial
        cbo_fiscal_idcondicioniva.EditValue = obj_config_empresa.fiscal_idcondicioniva
        'obj_config_empresa.fiscal_idsiglafiscal  = txt_fiscal_idsiglafiscal.
        txt_fiscal_numerofiscal.Text = obj_config_empresa.fiscal_numerofiscal
        cbo_fiscal_impresiontipo.EditValue = obj_config_empresa.fiscal_impresiontipo
        cbo_fiscal_impresorafiscal_modelo.Text = obj_config_empresa.fiscal_impresorafiscal_modelo
        cbo_fiscal_impresorafiscal_puerto.Text = obj_config_empresa.fiscal_impresorafiscal_puerto
        cbo_fiscal_impresorafiscal_velocidad.Text = obj_config_empresa.fiscal_impresorafiscal_velocidad


        If System.IO.File.Exists(txt_logogrande.Text) Then
            pic_logogrande.Image = Image.FromFile(txt_logogrande.Text)
        Else
            pic_logogrande.Image = Nothing
        End If

        If System.IO.File.Exists(txt_logochico.Text) Then
            pic_logochico.Image = Image.FromFile(txt_logochico.Text)
        Else
            pic_logochico.Image = Nothing
        End If


        flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False

        Me.Show()
    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_config_empresa.razonsocial = txt_razonsocial.Text
            obj_config_empresa.domicilio = txt_domicilio.Text
            obj_config_empresa.nombrefantasia = txt_nombrefantasia.Text
            obj_config_empresa.web = txt_web.Text
            obj_config_empresa.horario = txt_horario.Text
            obj_config_empresa.email = txt_email.Text
            obj_config_empresa.celular = txt_celular.Text
            obj_config_empresa.fax = txt_fax.Text
            obj_config_empresa.telefono = txt_telefono.Text
            obj_config_empresa.idpais = cbo_Pais.EditValue
            obj_config_empresa.idprovincia = cbo_Provincia.EditValue
            obj_config_empresa.logochico = txt_logochico.Text
            obj_config_empresa.logogrande = txt_logogrande.Text

            obj_config_empresa.fiscal_razonsocial = txt_fiscal_razonsocial.Text
            obj_config_empresa.fiscal_idcondicioniva = cbo_fiscal_idcondicioniva.EditValue
            'obj_config_empresa.fiscal_idsiglafiscal  = txt_fiscal_idsiglafiscal.
            obj_config_empresa.fiscal_numerofiscal = txt_fiscal_numerofiscal.Text
            obj_config_empresa.fiscal_impresiontipo = cbo_fiscal_impresiontipo.EditValue
            obj_config_empresa.fiscal_impresorafiscal_modelo = EnumDescription(CType(cbo_fiscal_impresorafiscal_modelo.EditValue, cls_config_empresa.enu_fiscalmodelovalor))
            obj_config_empresa.fiscal_impresorafiscal_puerto = cbo_fiscal_impresorafiscal_puerto.Text
            obj_config_empresa.fiscal_impresorafiscal_velocidad = cbo_fiscal_impresorafiscal_velocidad.Text

            obj_config_empresa.Modificar()

            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub txt_logogrande_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_logogrande.ButtonClick
        Dim OFD As New OpenFileDialog
        OFD.Filter = "Archivos de imagen(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|Todos los archivos (*.*)|*.*"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            txt_logogrande.Text = OFD.FileName
            txt_logogrande_Validated(Nothing, Nothing)
        End If
    End Sub

    Private Sub txt_logogrande_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_logogrande.Validated
        If System.IO.File.Exists(txt_logogrande.Text) Then
            pic_logogrande.Image = Image.FromFile(txt_logogrande.Text)
        Else
            pic_logogrande.Image = Nothing
        End If
    End Sub

    Private Sub txt_logochico_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_logochico.ButtonClick
        Dim OFD As New OpenFileDialog
        OFD.Filter = "Archivos de imagen(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|Todos los archivos (*.*)|*.*"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            txt_logochico.Text = OFD.FileName
            txt_logochico_Validated(Nothing, Nothing)
        End If
    End Sub

    Private Sub txt_logochico_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_logochico.Validated
        If System.IO.File.Exists(txt_logochico.Text) Then
            pic_logochico.Image = Image.FromFile(txt_logochico.Text)
        Else
            pic_logochico.Image = Nothing
        End If
    End Sub

    Private Sub cbo_fiscal_impresiontipo_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_fiscal_impresiontipo.EditValueChanged
        lcg_1.BeginUpdate()
        Select Case CType(cbo_fiscal_impresiontipo.EditValue, cls_config_empresa.enu_impresiontipo)
            Case cls_config_empresa.enu_impresiontipo.impresorafiscal
                lcg_impresoraFiscal.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always
            Case Else
                lcg_impresoraFiscal.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never
        End Select
        lcg_1.EndUpdate()
    End Sub

    Private Sub cbo_fiscal_idcondicioniva_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_fiscal_idcondicioniva.EditValueChanged
        Dim condicioniva As New cls_condicioniva_
        condicioniva.Buscar(cbo_fiscal_idcondicioniva.EditValue)
        If Not IsNothing(condicioniva.siglafiscal) Then
            txt_fiscal_idsiglafiscal.Text = condicioniva.siglafiscal.ToString
        Else
            txt_fiscal_idsiglafiscal.Text = ""
        End If
    End Sub
End Class